package app.controllers.admin.stat;

import app.models.wx.Menu;
import app.models.wx.VisitRecord;
import app.models.wx.VisitStat;
import com.github.sog.controller.BasicController;
import com.github.sog.controller.datatables.core.DatatablesCriterias;
import com.google.common.base.Strings;
import com.jfinal.ext.kit.DateKit;
import com.jfinal.plugin.activerecord.Page;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.util.Date;

/**
 * <p>
 *     模块访问记录
 * </p>
 *
 * @author Jerry Ou
 * @version 1.0 2014-02-12 13:41
 * @since JDK 1.6
 */
public class ModuleController extends BasicController {

    public void index() {
        render("index.ftl");
    }

    public void list() {
        final DatatablesCriterias criterias = getCriterias();
        Page<VisitStat> page = VisitStat.dao.datatable(criterias);

        renderDataTables(page, criterias);
    }

    public void record() {
        int visitMenu = getParaToInt("visitMenu", 0);
        String day = getPara("day");

        Menu menu = Menu.dao.findById(visitMenu);

        setAttr("menu", menu);
        if (menu != null) {
            setAttr("menuName", menu.get("name"));
        }
        setAttr("visitMenu", visitMenu);
        setAttr("day", day);
        render("record.ftl");
    }

    public void recordList() {
        int visitMenu = getParaToInt("visitMenu", 0);
        String day = getPara("day");
        if (Strings.isNullOrEmpty(day)) {
            Date now = new Date();
            day = DateFormatUtils.format(now, DateKit.dateFormat);
        }

        final DatatablesCriterias criterias = getCriterias();
        Page<VisitRecord> parkingPage = VisitRecord.dao.datatable(criterias, visitMenu, day);

        renderDataTables(parkingPage, criterias);
    }

}
